﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QuanLyPhongKham
{
    public partial class ucDmDoiTuong : BaseUserControl
    {
        private DAL.AccessData m;
        private string sql = "";
        private DataTable dt = new DataTable();
        public ucDmDoiTuong(DAL.AccessData acc)
        {
            InitializeComponent();
            m = acc;
            this.Title = "DM Đối tượng";
        }

        private void butMoi_Click(object sender, EventArgs e)
        {
            doituong.Enabled = true;
            doituong.Text = "";
            ena_button(false);
            madoituong.Text = m.get_capid_doituong.ToString();
            doituong.Focus();
        }

        private void butLuu_Click(object sender, EventArgs e)
        {
            if (doituong.Text == "")
            {
                doituong.Focus(); return;
            }
            if (madoituong.Text != "")
            {
                m.upd_dmdoituong(int.Parse(madoituong.Text), doituong.Text);
                doituong.Enabled = false;
                load_grid();
                ena_button(true);
                reft_text();
                
            }
        }
        private void load_grid()
        {
            sql = "select * from " + m.user + ".doituong order by madoituong";
            dt = m.get_data(sql).Tables[0];
            this.gridControl1.DataSource = dt;
        }
        
        public Color MyGetColorRowCol(int row, int col)
        {
            Color c = Color.Navy;
            //try
            //{
            //    if (row == dataGrid1.CurrentRowIndex)
            //    {
            //        dataGrid1.Select(row);
            //        c = Color.White;
            //    }
            //}
            //catch
            //{
            //}
            return c;
        }
        public delegate Color delegateGetColorRowCol(int row, int col);
        public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
        {
            private delegateGetColorRowCol _getColorRowCol;
            private int _column;
            public DataGridColoredTextBoxColumn(delegateGetColorRowCol getcolorRowCol, int column)
            {
                _getColorRowCol = getcolorRowCol;
                _column = column;
            }
            protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
            {
                try
                {
                    foreBrush = new SolidBrush(_getColorRowCol(rowNum, this._column));
                }
                catch { }
                finally
                {
                    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
                }
            }
        }
        private void ena_button(bool ena)
        {
            //butMoi.Enabled = ena;
            //butLuu.Enabled = !ena;
            //butSua.Enabled = ena;
            //butHuy.Enabled = ena;
            //butBoqua.Enabled = !ena;
            //butKetthuc.Enabled = ena;
        }
        private void butSua_Click(object sender, EventArgs e)
        {
            ena_button(false);
            doituong.Enabled = true;
            doituong.Focus();
        }

        private void butBoqua_Click(object sender, EventArgs e)
        {
            ena_button(true);
            doituong.Enabled = false;
        }

        private void butHuy_Click(object sender, EventArgs e)
        {
            if (madoituong.Text != "" && doituong.Text != "")
            {
                if (kiemtra(int.Parse(madoituong.Text)))
                {
                    MessageBox.Show("Danh mục đã sử dụng, không được huỷ!", DAL.AccessData.Msg); return;
                }
                if (MessageBox.Show("Bạn có đồng ý huỷ không?", DAL.AccessData.Msg, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {
                    sql = "delete from " + m.user + ".doituong where madoituong=" + int.Parse(madoituong.Text) ;
                    m.execute_data(sql);
                    load_grid();
                }
            }
        }
        private bool kiemtra(int ma)
        {
            sql = "select * from " + m.user + ".benhanpk where madoituong=" + ma;
            if (m.get_data(sql).Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        private void butKetthuc_Click(object sender, EventArgs e)
        {
            //fix:
            //this.Close();
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            reft_text();
            doituong.Enabled = false;
        }
        private void reft_text()
        {
            //try
            //{
            //    madoituong.Text = dataGrid1[dataGrid1.CurrentCell.RowNumber, 0].ToString();
            //    doituong.Text = dataGrid1[dataGrid1.CurrentCell.RowNumber, 1].ToString();
            //}
            //catch { madoituong.Text = ""; }
        }

        private void frmDmdoituong_Load(object sender, EventArgs e)
        {
            load_grid();
           
            doituong.Enabled = false;
            reft_text();
        }

        private void doituong_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
            }
        }


        public override string Validate()
        {
            return "Error";
        }

        private void crudOperation1_ButtonClick(object sender, DBOperateEventArgs e)
        {
            switch (e.ButtonKind) { 
                case ButtonType.Save:
                    var result = Common.NewProcessingDialog(this.FormMain);
                    Thread.Sleep(5000);
                    result.EndProcessing();
                    break;
                default:
                    break;

            }
        }
    }
}